﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Collections.Generic;

namespace Stip01.Config
{
    using System;
    using System.IO;

    public class Singleton
    {
        private static Singleton instance;

        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }

        public Database GetDatabase()
        {
            Database database;
                    
            System.IO.FileStream fileStream = new System.IO.FileStream("db.dat", System.IO.FileMode.Open);

            DataContractSerializer serializer = new DataContractSerializer(typeof(Database));

            fileStream.Seek(0, SeekOrigin.Begin);

            database = (Database)serializer.ReadObject(fileStream);

            return database;
        }

        public void SaveDatabase( Database database )
        {
            System.IO.FileStream fileStream = new System.IO.FileStream("db.dat", System.IO.FileMode.OpenOrCreate);

            DataContractSerializer serializer = new DataContractSerializer(typeof(Database));

            serializer.WriteObject(fileStream, database);
        }

    }
 

    [DataContract]
    public class Database
    {
        [DataMember]
        public List<Channel> Channels { get; set; }

    }

    [DataContract]
    public class Channel
    {
        [DataMember]
        public string Name { get; set; }
    }
}
